<?php

namespace iBrand\Store\Backend\Console;

use Illuminate\Console\Command;
use DB;

class SystemSettingMenusCommand extends Command
{
	protected $signature = 'import:system-setting-menus';

	protected $description = 'import system setting menus';

	public function handle()
	{
		$lastOrder = DB::table(config('admin.database.menu_table'))->max('order');
		$setParent = DB::table(config('admin.database.menu_table'))->where('title', '系统设置')->where('parent_id', 0)->first();
		if (!$setParent) {
			$settingParentId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => 0,
				'order'      => ++$lastOrder,
				'title'      => '系统设置',
				'icon'       => 'iconfont icon-xitongshezhi',
				'blank'      => 1,
				'uri'        => 'setting/payChannels',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$settingParentId = $setParent->id;
		}

		$weChatSetting = DB::table(config('admin.database.menu_table'))->where('title', '微信设置')->where('parent_id', $settingParentId)->first();
		if (!$weChatSetting) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $settingParentId,
				'order'      => ++$lastOrder,
				'title'      => '微信设置',
				'icon'       => 'iconfont icon-weixinguanli',
				'blank'      => 1,
				'uri'        => 'setting/wechat',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$paySetting = DB::table(config('admin.database.menu_table'))->where('title', '支付设置')->where('parent_id', $settingParentId)->first();
		if (!$paySetting) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $settingParentId,
				'order'      => ++$lastOrder,
				'title'      => '支付设置',
				'icon'       => 'iconfont icon-zhifushezhi',
				'blank'      => 1,
				'uri'        => 'setting/payChannels',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$themeSetting = DB::table(config('admin.database.menu_table'))->where('title', '主题设置')->where('parent_id', $settingParentId)->first();
		if (!$themeSetting) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $settingParentId,
				'order'      => ++$lastOrder,
				'title'      => '主题设置',
				'icon'       => 'iconfont icon-zhutishezhi',
				'blank'      => 1,
				'uri'        => 'setting/theme',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$templateMessageSetting = DB::table(config('admin.database.menu_table'))->where('title', '模板消息设置')->where('parent_id', $settingParentId)->first();
		if (!$templateMessageSetting) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $settingParentId,
				'order'      => ++$lastOrder,
				'title'      => '模板消息设置',
				'icon'       => 'iconfont icon-weixinguanli',
				'blank'      => 1,
				'uri'        => 'setting/wechat/message',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$smsSetting = DB::table(config('admin.database.menu_table'))->where('title', '短信设置')->where('parent_id', $settingParentId)->first();
		if (!$smsSetting) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $settingParentId,
				'order'      => ++$lastOrder,
				'title'      => '短信设置',
				'icon'       => 'iconfont icon-duanxinshezhi',
				'blank'      => 1,
				'uri'        => 'setting/sms',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$backendSetting = DB::table(config('admin.database.menu_table'))->where('title', '后台设置')->where('parent_id', $settingParentId)->first();
		if (!$backendSetting) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $settingParentId,
				'order'      => ++$lastOrder,
				'title'      => '后台设置',
				'icon'       => 'iconfont icon-houtaiguanli',
				'blank'      => 1,
				'uri'        => 'setting/backend',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$statisticsSetting = DB::table(config('admin.database.menu_table'))->where('title', '网站统计设置')->where('parent_id', $settingParentId)->first();
		if (!$statisticsSetting) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $settingParentId,
				'order'      => ++$lastOrder,
				'title'      => '网站统计设置',
				'icon'       => 'iconfont icon-wangluotongjipeizhi',
				'blank'      => 1,
				'uri'        => 'setting/analytics',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$secureSetting = DB::table(config('admin.database.menu_table'))->where('title', '验证文件')->where('parent_id', $settingParentId)->first();
		if (!$secureSetting) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $settingParentId,
				'order'      => ++$lastOrder,
				'title'      => '验证文件',
				'icon'       => 'iconfont icon-shangchuanrenzhengwenjian',
				'blank'      => 1,
				'uri'        => 'setting/uploads',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}
	}
}